/**
 * yangtao
 */

$(document).ready(function() {

	//	$('header div:last-child').on('click', function() {
	//		var nHtml = '<input type="text" name="searh" style="width:' + ($(this).width() - 60) + 'px;line-height:14px;"><span id="search" class="glyphicon glyphicon-search" aria-hidden="true" style="margin-left:10px;"></span>';
	//		$(this).html(nHtml);
	//	});

	$('.home').on('click', function() {
		alert("sdaaaaadasda");
	})

	function fillUL(data, isFull) {
		var list = new Array();
	}

	$(document).on('click', '#enlarge', function() {
		$('#enlarge').css({
			'display': 'none'
		});
		$('#narrow').css({
			'display': 'block'
		});

		var w = $(window).width() + "px";
		var h = $(window).height() + "px";
		$('#remoteVideo').css({
			"height": h
		});

		layer.open({
			type: 1,
			title: false,
			closeBtn: 0,
			shadeClose: true,
			area: [w, h], //宽高
			content: $('.videoContainer').html()
		});
	});

	$(document).on('click', '#narrow,#close', function() {
		$('#narrow').css({
			'display': 'none'
		});
		$('#enlarge').css({
			'display': 'block'
		});
		$('#remoteVideo').css({
			"height": ""
		});
		layer.closeAll();
	});

	//	$(window).on('resize', function() {
	//		var h = $(window).height() + "px";
	//		$('#remoteVideo').css({"height": h});
	//	})

	// grab the room from the URL
	var room = location.search && location.search.split('?')[1];

	// create our webrtc connection
	var webrtc = new SimpleWebRTC({
		// the id/element dom element that will hold "our" video
		localVideoEl: 'localVideo',
		// the id/element dom element that will hold remote videos
		remoteVideosEl: 'remoteVideo',
		// immediately ask for camera access
		autoRequestMedia: true,
		debug: true,
		detectSpeakingEvents: true,
		autoAdjustMic: false,
		url: 'stun:115.159.47.80:3478'
	});

	if(!room) {
		$('form').submit(function() {
			// var val = $('#sessionInput').val().toLowerCase().replace(/\s/g, '-').replace(/[^A-Za-z0-9_\-]/g, '');
			var val = "qidiTalky";
			webrtc.createRoom(val, function(err, name) {
				if(!err) {
					console.log("roomId: " + val + "has been created!");
				} else {
					console.log(err);
				}
			});
			return false;
		});
	}

	// when it's ready, join if we got a room from the URL
	webrtc.on('readyToCall', function() {
		// you can name it anything
		if(room) webrtc.joinRoom(room);
	});

	// we got access to the camera
	webrtc.on('localStream', function(stream) {
		var button = document.querySelector('form>button');
		if(button) button.removeAttribute('disabled');
	});
	// we did not get access to the camera
	webrtc.on('localMediaError', function(err) {});

	// a peer video has been added
	webrtc.on('videoAdded', function(video, peer) {
		console.log('video added', peer);
		var remotes = document.getElementById('remotes');
		if(remotes) {
			var container = document.createElement('div');
			container.className = 'videoContainer';
			container.id = 'container_' + webrtc.getDomId(peer);
			container.appendChild(video);

			// suppress contextmenu
			video.oncontextmenu = function() {
				return false;
			};

			// resize the video on click
			video.onclick = function() {
				container.style.width = video.videoWidth + 'px';
				container.style.height = video.videoHeight + 'px';
			};

			// show the remote volume
			var vol = document.createElement('meter');
			vol.id = 'volume_' + peer.id;
			vol.className = 'volume';
			vol.min = -45;
			vol.max = -20;
			vol.low = -40;
			vol.high = -25;
			container.appendChild(vol);

			// show the ice connection state
			if(peer && peer.pc) {
				var connstate = document.createElement('div');
				connstate.className = 'connectionstate';
				container.appendChild(connstate);
				peer.pc.on('iceConnectionStateChange', function(event) {
					switch(peer.pc.iceConnectionState) {
						case 'checking':
							connstate.innerText = 'Connecting to peer...';
							break;
						case 'connected':
						case 'completed': // on caller side
							$(vol).show();
							connstate.innerText = 'Connection established.';
							break;
						case 'disconnected':
							connstate.innerText = 'Disconnected.';
							break;
						case 'failed':
							connstate.innerText = 'Connection failed.';
							break;
						case 'closed':
							connstate.innerText = 'Connection closed.';
							break;
					}
				});
			}
			remotes.appendChild(container);
		}
	});
	// a peer was removed
	webrtc.on('videoRemoved', function(video, peer) {
		console.log('video removed ', peer);
		var remotes = document.getElementById('remotes');
		var el = document.getElementById(peer ? 'container_' + webrtc.getDomId(peer) : 'localScreenContainer');
		if(remotes && el) {
			remotes.removeChild(el);
		}
	});

	// local p2p/ice failure
	webrtc.on('iceFailed', function(peer) {
		var connstate = document.querySelector('#container_' + webrtc.getDomId(peer) + ' .connectionstate');
		console.log('local fail', connstate);
		if(connstate) {
			connstate.innerText = 'Connection failed.';
			fileinput.disabled = 'disabled';
		}
	});

	// remote p2p/ice failure
	webrtc.on('connectivityError', function(peer) {
		var connstate = document.querySelector('#container_' + webrtc.getDomId(peer) + ' .connectionstate');
		console.log('remote fail', connstate);
		if(connstate) {
			connstate.innerText = 'Connection failed.';
			fileinput.disabled = 'disabled';
		}
	});
});